<?php

namespace app\admin\model\shop\user;

use app\admin\model\recommend\Level;
use app\admin\model\recommend\Partner;
use app\admin\model\shop\Paidan;
use think\facade\Db;
use think\Model;

/**
 * User
 */
class User extends Model
{
    // 表名
    protected $name = 'shop_user';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    public function getAvatarAttr($value): string
    {
        return full_url($value, false, config('buildadmin.default_avatar'));
    }

    public function setAvatarAttr($value): string
    {
        return $value == full_url('', false, config('buildadmin.default_avatar')) ? '' : $value;
    }

    public function getMoneyAttr($value): string
    {
        return bcdiv($value, 100, 2);
    }

    public function setMoneyAttr($value): string
    {
        return bcmul($value, 100, 2);
    }

    public function partner(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(Partner::class, 'partner_id', 'id');
    }
    public function level(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(Level::class, 'level_id', 'id');
    }
    public function shopUser(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(\app\admin\model\shop\user\User::class, 'pid', 'id');
    }

    public function inviteAfter($user_id,$pid)
    {
        Db::name('shop_user_recommend')->insertGetId(['user_id'=>$user_id,'pid'=>$pid,'ctime'=>time()]);
        $info = $this->where('id', $user_id)->find();
        $config = Db::name('recommend_setting')->where('name','config')->value('value');
        if (isset($info['pid']) && $info['pid']!='' && isset($config)) {
            $config = json_decode($config,true);
            $count = Db::name('shop_user_recommend')->where('pid',$info['pid'])->count();
            $pnum = 0;
            foreach ($config as $k=>$v){
                if($v['key'] == $count){
                    $pnum = $v['value'];
                }
            }
            $pinfo = self::where('id',$info['pid'])->find();
            if($pnum == 0){
                if($pinfo['level_id'] == 1){
                    $pnum = Db::name('recommend_setting')->where('name','daren_num')->value('value');
                }elseif ($pinfo['level_id'] == 2){
                    $pnum = Db::name('recommend_setting')->where('name','qps_num')->value('value');
                }elseif ($pinfo['level_id'] == 3){
                    $pnum = Db::name('recommend_setting')->where('name','xps_num')->value('value');
                }elseif ($pinfo['level_id'] == 4){
                    $pnum = Db::name('recommend_setting')->where('name','fws_num')->value('value');
                }
            }
            if($pinfo['pstatus'] == 1){
                $paidanModel = new Paidan();
                $res = $paidanModel->change($info['pid'],$paidanModel::TYPE_RECOMMEND,$pnum,$user_id,'');
                if(!$res['status']){
                    return false;
                }
            }
        }
        return true;
    }
}